package com.hstcs.systmes;

import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.math.Vector2;
import com.hstcs.assets.Config;
import com.hstcs.components.CameraComponent;
import com.hstcs.components.TransformComponent;


public class CameraSystem extends IteratingSystem {
	
	private ComponentMapper<TransformComponent> tm;
	private ComponentMapper<CameraComponent> cm;
	
	private static Vector2 tmpPos = new Vector2();
	private static Vector2 cameraPos = new Vector2();
	
			
	public CameraSystem() {
		super(Family.all(CameraComponent.class).get());
		
		tm = ComponentMapper.getFor(TransformComponent.class);
		cm = ComponentMapper.getFor(CameraComponent.class);
	}

	@Override
	public void processEntity(Entity entity, float deltaTime) {
		CameraComponent cam = cm.get(entity);
		
		if (cam.target == null) {
			return;
		}
		
		TransformComponent target = tm.get(cam.target);
		
		if (target == null) {
			return;
		}
		

		cam.camera.position.x =  target.pos.x;
		cam.camera.position.y = target.pos.y;

	} 
	
	public static  Vector2 ScreenToMap(float x, float y){		
		return tmpPos.set(cameraPos.x + x - Config._WIDTH/2, cameraPos.y + y - Config._HEIGHT/2);
	}
	
	public static Vector2 MapToScreen(float x, float y){
		return tmpPos.set(x- cameraPos.x + Config._WIDTH/2, y - cameraPos.y + Config._HEIGHT/2);
	}
}
